<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>最长字符串链</title>
</head>
<body>
  <script>
    function fn(words) {
      words.sort((a, b) => a.length - b.length)
      const map = new Map()
      let max = 0
      for (const word of words) {
        let cnt = 1
        for (let i = 0; i < word.length; i++) {
          const x = word.substring(0, i) + word.substring(i + 1)
          if (map.has(x)) {
            cnt = Math.max(map.get(x) + 1, cnt)
          }
        }
        map.set(word, cnt)
        max = Math.max(max, cnt)
      }
      return max
    }
    console.log(fn(['a', 'b', 'ba', 'bca', 'bda', 'bdca']))
  </script>
</body>
</html>